package com.wangxiang.shop.product.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangxiang.common.utils.*;
import com.wangxiang.shop.product.dao.ClothingSkuDao;
import com.wangxiang.shop.product.entity.ClothingSkuEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wangxiang.shop.product.dao.ClothingSpuDao;
import com.wangxiang.shop.product.entity.ClothingSpuEntity;
import com.wangxiang.shop.product.service.ClothingSpuService;


@Service("clothingSpuService")
public class ClothingSpuServiceImpl extends ServiceImpl<ClothingSpuDao, ClothingSpuEntity> implements ClothingSpuService {
    @Autowired
    private ClothingSpuDao clothingSpuDao;
    @Autowired
    private ClothingSkuDao clothingSkuDao;



    @Override
    public PageEntity page(PageEntity pageEntity) {
        PageHelper.startPage(pageEntity.getPageNum(),pageEntity.getPageSize());
        //查所有
        List<ClothingSpuEntity> clothingSpuEntities = clothingSpuDao.selectList(null);
        PageInfo<ClothingSpuEntity> clothingSpuEntityPageInfo = new PageInfo<>(clothingSpuEntities);
        //使用自定义的返回值
        PageGenerate pageGenerate = new PageGenerate();
        PageEntity page = pageGenerate.generate(clothingSpuEntityPageInfo);

        return page;

    }

    @Override
    public PageEntity search(PageEntity pageEntity) {
        PageHelper.startPage(pageEntity.getPageNum(),pageEntity.getPageSize());
        StrUtil strUtil = new StrUtil();
        String like = strUtil.likeStr(pageEntity.getLike());
        List<ClothingSpuEntity> search1 = clothingSpuDao.search(like);
        PageInfo<ClothingSpuEntity> clothingSpuEntityPageInfo = new PageInfo<>(search1);
        PageGenerate pageGenerate = new PageGenerate();
        PageEntity generate = pageGenerate.generate(clothingSpuEntityPageInfo);
        return generate;
    }

    @Override
    public ClothingSpuEntity getEntity(ClothingSpuEntity clothingSpuEntity) {
        ClothingSpuEntity clothingSpuEntities = clothingSpuDao.get(clothingSpuEntity.getId());
        StrChangeMapUtil strChangeMapUtil = new StrChangeMapUtil();
        HashMap<String, Object> map = strChangeMapUtil.map(clothingSpuEntities.getParamMap());
        mapListUtils mapListUtils = new mapListUtils();
        List<HashMap<String, Object>> list = mapListUtils.ListUtil(map);
        clothingSpuEntities.setListMap(list);
        return clothingSpuEntities;
    }
    //更新
    @Override
    public int change(ClothingSpuEntity clothingSpuEntity) {
        ClothingSpuEntity spuEntity = new ClothingSpuEntity();
        if (clothingSpuEntity.getBrand() != ""){
            spuEntity.setBrand(clothingSpuEntity.getBrand());
        }
        if (clothingSpuEntity.getTitle() != "") {
            spuEntity.setTitle(clothingSpuEntity.getTitle());
        }
        if (clothingSpuEntity.getType() != ""){
            spuEntity.setType(clothingSpuEntity.getType());
        }
        if (clothingSpuEntity.getPrice() != 0){
            spuEntity.setPrice(clothingSpuEntity.getPrice());
        }
        spuEntity.setId(clothingSpuEntity.getId());
        spuEntity.setImage(clothingSpuEntity.getImage());

        int i = clothingSpuDao.updateById(spuEntity);
        return i;
    }

    @Override
    public int add(ClothingSpuEntity clothingSpuEntity) {
        //处理数据
        ClothingSpuEntity spuEntity = new ClothingSpuEntity();
        //拿出规则数据 title,type,brand,price,image
        spuEntity.setTitle(clothingSpuEntity.getTitle());
        spuEntity.setType(clothingSpuEntity.getType());
        spuEntity.setBrand(clothingSpuEntity.getBrand());
        spuEntity.setPrice(clothingSpuEntity.getPrice());
        spuEntity.setImage(clothingSpuEntity.getImage());



        //处理参数，去掉null的key,value
        StrChangeMapUtil strChangeMapUtil = new StrChangeMapUtil();
        String param = strChangeMapUtil.devStr(clothingSpuEntity.getParamMap());
        spuEntity.setParamMap(param);

        //颜色，尺寸，数量集合
        List<HashMap<String, Object>> list = strChangeMapUtil.devColorSizeNum(clothingSpuEntity.getDevColorSizeNum());
        ArrayList<ClothingSkuEntity> sku = new ArrayList<>();
        list.forEach((item)->{
            ClothingSkuEntity clothingSkuEntity = new ClothingSkuEntity();
            //颜色集合
            if (item.get("colorList")!=null){
                spuEntity.setColorList(item.get("colorList").toString());
            }
            //尺寸集合
            if (item.get("sizeList")!=null){
                spuEntity.setSizeList(item.get("sizeList").toString());
            }
            if (item.get("color")!=null){
                clothingSkuEntity.setColor(item.get("color").toString());
            }
            if (item.get("size")!=null){
                clothingSkuEntity.setSize(item.get("size").toString());
            }
            if (item.get("num")!=null){
                clothingSkuEntity.setWare(Integer.valueOf(item.get("num").toString()));
            }
            sku.add(clothingSkuEntity);
        });
        spuEntity.setStatus(2);
        int insert = clothingSpuDao.insertGetKey(spuEntity);
        if (insert>0){
            for (int i = 0; i < sku.size(); i++) {
                if (sku.get(i).getColor()!=null&sku.get(i).getSize()!=null&sku.get(i).getWare()!=0){
                    sku.get(i).setSpuId(spuEntity.getId());
                    sku.get(i).setBrand(spuEntity.getBrand());
                    sku.get(i).setType(spuEntity.getType());
                    sku.get(i).setStatus(2); //待审核
                    int insert1 = clothingSkuDao.insert(sku.get(i));
                    if (insert1==0){
                        return 3;
                    }
                }
            }
        }else {
            return 2;
        }

        return spuEntity.getId();
    }

    @Override
    public int invalid(ClothingSpuEntity clothingSpuEntity) {
        ClothingSpuEntity spuEntity = new ClothingSpuEntity();
        spuEntity.setId(clothingSpuEntity.getId());
        spuEntity.setStatus(1);
        int i = clothingSpuDao.updateById(spuEntity);
        if (i>0){
            ClothingSkuEntity clothingSkuEntity = new ClothingSkuEntity();
            clothingSkuEntity.setSpuId(spuEntity.getId());
            clothingSkuEntity.setStatus(1);
            int invalid = clothingSkuDao.upDateStatus(clothingSkuEntity);
            if (invalid>0){
                return 0;
            }else {
                return 1;
            }
        }
        if (i==0){
            return 2;
        }
        return 0;
    }

    /**
     * 商品状态回复，需要审核,涉及sku表
     * 状态置为 待审核，2
     * @param clothingSpuEntity
     * @return
     */
    @Override
    public int reUpShop(ClothingSpuEntity clothingSpuEntity) {
        ClothingSpuEntity spuEntity = new ClothingSpuEntity();
        spuEntity.setId(clothingSpuEntity.getId());
        spuEntity.setStatus(2);
        int i = clothingSpuDao.updateById(spuEntity);
        if (1>0){
            ClothingSkuEntity skuEntity = new ClothingSkuEntity();
            skuEntity.setSpuId(clothingSpuEntity.getId());
            skuEntity.setStatus(2);
            int i1 = clothingSkuDao.upDateStatus(skuEntity);
            System.out.println(i1);
            if (i1>0){
                return 0;
            }else {
                return 1;
            }
        }
        if (i==0){
            return 2;
        }

        return 0;
    }

    /**
     * 外部接口调用
     * @param
     * @return
     */
    @Override
    public HashMap<String, String> getMap(int spuId,int roleId) {
        HashMap<String, String> map = new HashMap<>();
        //查询记录
        ClothingSpuEntity spuEntity = clothingSpuDao.selectById(spuId);
        if (spuEntity!=null){
            map.put("shopId",String.valueOf(spuId));
            map.put("roleId",String.valueOf(roleId));
            map.put("shopType",spuEntity.getType());
            map.put("shopDbName","clothing_spu");
        }
        //拿商品名称
        if (spuEntity.getParamMap()!=null|spuEntity.getParamMap()!= "="){
            StrChangeMapUtil strChangeMapUtil = new StrChangeMapUtil();
            HashMap<String, Object> paramMap = strChangeMapUtil.map(spuEntity.getParamMap());
            if (paramMap.get("商品名称")!=null){
                map.put("shopName",paramMap.get("商品名称").toString());
            }
        }



        return map;
    }
}